import 'dart:math';

import 'package:flutter/material.dart';
import 'package:junyeyishu/utils/utils.dart';
import 'package:junyeyishu/widgets/QGWidget.dart';

class QGSortPriceWidget extends StatefulWidget {
  int current;

  List<String> items;
  MainAxisAlignment alignment;
  Function(int index) onSwitch;

  QGSortPriceWidget(
      {Key? key,
      required this.current,
      required this.items,
      required this.onSwitch,
      this.alignment = MainAxisAlignment.spaceBetween})
      : super(key: key);

  @override
  State<QGSortPriceWidget> createState() => _QGSortPriceWidgetState();
}

class _QGSortPriceWidgetState extends State<QGSortPriceWidget> {
  @override
  Widget build(BuildContext context) {
    List<Widget> children = [];
    for (int i = 0; i < widget.items.length; i++) {
      var child = GestureDetector(
        onTap: () {
          widget.onSwitch(i);
        },
        child: Container(
          height: 24.rpx,
          margin: EdgeInsets.only(left: 12.rpx),
          padding: EdgeInsets.symmetric(horizontal: 14.rpx),
          decoration: i == widget.current
              ? BoxDecoration(
                  color: const Color(0xFF111111),
                  borderRadius: BorderRadius.circular(6.rpx),
                )
              : BoxDecoration(
                  color: Colors.transparent,
                  border: Border.all(
                      width: 0.5.rpx, color: const Color(0xFF111111)),
                  borderRadius: BorderRadius.circular(6.rpx),
                ),
          alignment: Alignment.center,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                widget.items[i],
                style: TextStyle(
                    color: (i == widget.current)
                        ? const Color(0xFFDEFF80)
                        : const Color(0xFF111111),
                    fontSize: 11.rpx,
                    fontWeight: (i == widget.current)
                        ? FontWeight.bold
                        : FontWeight.normal),
              ),
              SizedBox(width: 4.rpx),
              i == widget.current
                  ? i == 0
                      ? Transform.rotate(
                          angle: pi,
                          child: QGImage(asset: "sale_triangle_active"))
                      : QGImage(asset: "sale_triangle_active")
                  : i == 0
                      ? Transform.rotate(
                          angle: pi, child: QGImage(asset: "sale_triangle"))
                      : QGImage(asset: "sale_triangle")
            ],
          ),
        ),
      );
      children.add(child);
    }

    return Row(mainAxisAlignment: MainAxisAlignment.start, children: children);
  }
}
